package com.rp.configure.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rp.common.result.ResultDTO;
import com.rp.common.utils.Selectway;
import com.rp.configure.entity.Device;
import com.rp.configure.entity.DeviceAttribute;
import com.rp.configure.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author vicente
 * @since 2021-07-16
 */
@RestController
@RequestMapping("/device")
@Api(tags="设备管理")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    @Autowired
    private Selectway selectway;

    @ApiOperation(value = "分页列表")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    @GetMapping(value = "/list_page")
    public ResultDTO<IPage<Device>> listPage(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
                                 @RequestParam(value = "gatewayId",required = false)  String gatewayId) {
//        QueryWrapper<Device> queryWrapper = new QueryWrapper<Device>();
////        selectway.fields(queryWrapper, string,"gateway",null,null);
//        queryWrapper.orderByDesc("create_time").eq("gateway_id",gatewayId);
        IPage<Device> deviceIPage=deviceService.listPage(new Page(pageNum, pageSize),gatewayId);
        return ResultDTO.success().data(deviceIPage);
    }
    @ApiOperation(value = "列表")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    @GetMapping(value = "/list")
    public ResultDTO<List<Device>> list() {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<Device>();
//        selectway.fields(queryWrapper, string,"gateway",null,null);
        queryWrapper.orderByDesc("create_time");
        return ResultDTO.success().data(deviceService.list(queryWrapper));
    }
    @ApiOperation(value = "不分页列表根据网关id查询")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    @GetMapping(value = "/list_gateway")
    public ResultDTO<List<DeviceAttribute>> listByGateway(@RequestParam(value = "gatewayId",required = false) String gatewayId) {
        QueryWrapper<DeviceAttribute> queryWrapper = new QueryWrapper<DeviceAttribute>();
        queryWrapper.orderByDesc("create_time").eq("gateway_id",gatewayId);
        return ResultDTO.success().data(deviceService.listByGateway(queryWrapper));
    }


    @PostMapping("/insert")
    @ApiOperation(value = "增加")
    @Transactional
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO insert(@RequestBody Device device) {
        return ResultDTO.tf(deviceService.save(device));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO update(@RequestBody Device device) {
        return ResultDTO.tf(deviceService.updateById(device));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO delete(@RequestBody @ApiParam(value = "id", required = true) String id) {
        return ResultDTO.tf(deviceService.removeById(id));
    }

    @DeleteMapping("/deleteAll")
    @ApiOperation(value = "批量删除")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO deleteAll(@RequestBody @ApiParam(value = "id(英文逗号隔开)", required = true) String ids) {
        return ResultDTO.tf(deviceService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
